BufferedWriter bf = new BufferedWriter(new FileWriter(new File(args[1])))

// read file linewise and just drop those which aren't annotated with MP
new File(args[0]).readLines().each { line ->
  def mp = ""
  def l = []

  // ignore commented lines
  if ( !line.startsWith("!") ) {

    // reset params ...
    //if ( line.trim().equals("[Term]") ) {

      // if ( !mp.equals("") ) println mp + "\t" + l.join("\t")
      // println mp + "\t" + l.join("\t")

     

    //} else {
      // term starts
      if ( line.startsWith("id: MP:") ) {
	if ( line.contains("MP:") && line.contains("!")) mp = line.substring(line.indexOf("MP:"), line.indexOf("!")).trim()
        else if ( line.contains("MP:") && line.contains("!")) mp = line.substring(line.indexOf("MP:")).trim()
	bf << "\n"
        bf << mp       
      } else if ( line.startsWith("intersection_of:") ) {
        if ( line.contains("MA:") && line.contains("!")) {
           bf << "\t"
           bf << line.substring(line.indexOf("MA:"), line.indexOf("!")).trim()
        } else if ( line.contains("MA:")) {
           bf << "\t"
           bf << line.substring(line.indexOf("MA:"))
        } else if ( line.contains("PATO:") && line.contains("!")) {
           bf << "\t"
           bf << line.substring(line.indexOf("PATO:"), line.indexOf("!")).trim()
        } else if ( line.contains("PATO:")) {
          bf << "\t"
          bf << line.substring(line.indexOf("PATO:"))
        } else if ( line.contains("GO:") && line.contains("!")) {
          bf << "\t"
          bf << line.substring(line.indexOf("GO:"), line.indexOf("!")).trim()
        } else if ( line.contains("GO:")) {
          bf << "\t"
          bf << line.substring(line.indexOf("GO:"))
        }
      }

    //}
  }
}

bf.flush()
bf.close()